#include "stdio.h"
#include "math.h"
#include "string.h"
#include "stdlib.h"

int main()
{
	float tu[6][6];
	int n;
	int city[20],num;
	int a,i,j;
	float money;
	char s[20];
	
	scanf("%d",&n);
	for (a=1;a<=n;++a)
	{
		memset(tu,0,sizeof(tu));	
		for (i=1;i<=5;++i)
			for (j=1;j<=5;++j)
			{
				scanf("%f",&tu[i][j]);
			}
		while (1==1)
		{	
		memset(city,0,sizeof(city));
		scanf("%d",&num);
		if (num==0) break;
		for (i=1;i<=num;++i) scanf("%d",&city[i]);
		city[0]=1;
		city[++num]=1;
		scanf("%f",&money);
		for (i=1;i<=num;++i)
		{
			money*=tu[ city[i-1] ][ city[i] ];
			money=floor((money*100)+0.5)*1.0/100;
		}
		printf("%.2f\n",money);
		}
		if (a!=n) printf("\n");
	}
	return 0;
}
